package changceilingcolor003;
import toxi.geom.Vec3D;
import processing.core.PApplet;

public class Grid {
	ChangCeilingColor003 p5;
	int idX;
	int idY;
	Pt[][] cells;

	Grid(ChangCeilingColor003 _p5, int _idX, int _idY) {

		p5 = _p5;
		idX= _idX;
		idY= _idY;
		cells = new Pt[idX][idY];
	}

	void init() {
		for (int i=0;i<p5.COLS; i++) {
			for (int j=0;j<p5.ROWS; j++) {
				Vec3D v=new Vec3D(i*p5.resulotion, j*p5.resulotion, 0); 
				int colR=0;
				int colG=0;
				int colB=0;
				//int Transparancy=50;
				int Transparancy=0;
				cells[i][j]=new Pt(v,colR,colG,colB,Transparancy,p5);
			}
		}
	}




	void run() {

		      
		
		
		
		if(!p5.SomehowWorkFast)////////////////////////////////////////////////////////////
		{

			//people leave impact on the whole ground 
			for (int i=0;i<p5.alisPeop.size();i++) {
				Person p=(Person)p5.alisPeop.get(i);
				//	      Vec3D n = pTerr.getLocInGrid(p.pos);
				int ptX = p5.pTerr.xLocInGrid(p.GetLocation());
				int ptY = p5.pTerr.yLocInGrid(p.GetLocation());

				if (cells[ptX][ptY].Transparancy <= 100 && cells[ptX][ptY].Transparancy >= 0 ) {
					//cells[ptX][ptY].Transparancy +=1;
					cells[ptX][ptY].Transparancy +=30;
					cells[ptX][ptY].colG +=3;
					cells[ptX][ptY].StepCount +=0.2;//////////////////////////////////////////////////////////////////////
				}
			}

			//adding the grid point to attraction sets/count the points
			for(int i =0; i<p5.alisDst.size(); ++i)
			{	
				int count=0;
				int countpeople=0;
				Attraction att =  (Attraction) p5.alisDst.get(i);
				//int ptX = p5.pTerr.xLocInGrid(att.LocationDelta);
				//int ptY = p5.pTerr.yLocInGrid(att.LocationDelta);
				//int ptX = p5.pTerr.xLocInGrid(att.GetLocation());
				//int ptY = p5.pTerr.yLocInGrid(att.GetLocation());
				if(att.m_active==true){
					for (int j=0;j<p5.COLS; j++) {
						for (int k=0;k<p5.ROWS; k++) {					
							//cells[j][k].run();
							Vec3D desired = (Vec3D)cells[j][k].loc.sub(att.GetLocation());
							float hDist = desired.magnitude();			
							if(hDist<att.GetAttractionRadious())
							{	
								cells[j][k].colR +=att.GetColorR();
								cells[j][k].colG +=att.GetColorG();
								cells[j][k].colB +=att.GetColorB();
								//cells[j][k].colR =att.GetColorR();
								//cells[j][k].colG =att.GetColorG();
								//cells[j][k].colB =att.GetColorB();
								count++;
							}

						}

					}

					//count people in one program
					for (int j=0;j<p5.alisPeop.size();j++) {
						Person p=(Person)p5.alisPeop.get(j);
						Vec3D desired = (Vec3D)p.GetLocation().sub(att.GetLocation());
						float hDist = desired.magnitude();			
						if(hDist<att.GetAttractionRadious()){
							countpeople++;
						}

					}
					att.output_area=count;
					att.output_countpeople=countpeople;
					p5.stroke(255,255,255);
				/*	
					p5.translate(0,0, att.GetLocation().z);
					//if(p5.showDetail==true){
					//p5.text("area:"+count+"\n"+"people:"+countpeople,att.GetLocation().x+50,att.GetLocation().y);
					p5.fill(255);
					p5.text("Usage:"+(count/(countpeople+1))+"\n"+"Density:"+countpeople,att.GetLocation().x+180,att.GetLocation().y);
					//}
					p5.stroke(0,255,255);
				    p5.strokeWeight(1);	   
                    p5.line(att.GetLocation().x+180,att.GetLocation().y, att.GetLocation().x+5,att.GetLocation().y);
                    p5.translate(0,0, -att.GetLocation().z);
                    
                    */
		
				}

			}
			
			for (int j=0;j<p5.COLS; j++) {
				for (int k=0;k<p5.ROWS; k++) {					
					cells[j][k].run();
				}

			}
			
		}
		

		
		
	}


	void clear() {
		for (int i=0;i<p5.COLS; i++) {
			for (int j=0;j<p5.ROWS; j++) {
				cells[i][j].colR=0;
				cells[i][j].colG=0;
				cells[i][j].colB=0;

			}
		}
	}

}
